package com.yinhai.util;

import java.lang.Thread;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class XScheduler {
   	
	 private ScheduleListener mListener = null;
	 private ScheduleThread mThread = null;
	 private ScheduleHandler mHandler = null;
	 
	 public XScheduler(){
		
	 }

	 public void setScheduleListener(ScheduleListener listener){
		 mListener = listener;
		 if(this.mHandler == null ){
			  mHandler = new ScheduleHandler(this);
		  }
	 }
	 
	 public ScheduleListener getListener(){
		 return this.mListener;
	 }
	 
	 
	 //times :1 singleshot ; times: -1 forever
	 public void startSechedule(long period, int times){
		 stop();
		 mThread = new ScheduleThread(period,period,times,this);
		 mThread.startThread();
	 }
	
	 public void startSechedule(long start,long period, int times){
		 stop();
		 mThread = new ScheduleThread(start,period,times,this);
		 mThread.startThread();
	 }
	 
	 public void stop(){
		 if(mThread != null){
		     mThread.stopThread();
		 }
	 }
	 
	 public ScheduleHandler getHandler(){
		 return this.mHandler;
	 }
	 
	 
	 public static class ScheduleHandler extends Handler{

		 private XScheduler mScheduler;
		 
		public ScheduleHandler(XScheduler scheduler) {
			super();
		    mScheduler = scheduler;
		}

        
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if(mScheduler.getListener() != null){
				mScheduler.getListener().handleMessage(msg);
			}
		}
		
		
	 }
	 
	 public static interface ScheduleListener {
		 public void handleMessage(Message msg) ;
     }
 
	 
	 @SuppressWarnings("unused")
	static private  class ScheduleThread extends Thread{
		 
		 private boolean mStopped = true;		 
		 private int mRepeattimes = -1;
		 private int mRemaintimes = -1;
		 private long mPeriod= 0;
		 private  XScheduler mParent;
		 private long mStartPeriod = -1;
		 
		   public ScheduleThread(long startperiod,long period,int repeattimes,XScheduler parent){
			   super();		 
			   mStartPeriod = startperiod;
			   mRepeattimes = repeattimes;
			   mRemaintimes = mRepeattimes;
			   mParent = parent;
			   mPeriod = period;
		   }

		   public void startThread(){
			     mStopped = false;
			     this.start();
		   }
		@Override
		public void run() {
			// TODO Auto-generated method stub
			 try{	
				 if(mStopped){
					    return;		 	
				  }	
				 Thread.sleep(mStartPeriod);
				 if(mRemaintimes != -1){  //-1��ʾ��Զ��Ч
	    	    	 mRemaintimes -- ;
			      }
				 sendMessage();				 
				while(mRemaintimes != 0){
						if(mStopped){
							break;				 	
						 }	
		    	         Thread.sleep(mPeriod);
			    	     if(mRemaintimes != -1){
			    	    	 mRemaintimes -- ;
					      }
			    		 sendMessage();
					}
		          }catch(InterruptedException  e){
		        	  e.printStackTrace();
		        	//  mRemaintimes -- ;
		          }
			      
			}
		
		 private void sendMessage(){
			 if(mParent.getHandler() != null && !mStopped){
				 Message msg = new Message();
				 Bundle b = new Bundle(); 
		         b.putInt("remaintimes", this.mRemaintimes);
		         b.putInt("repeattimes", this.mRepeattimes);
				 mParent.getHandler().sendMessage(msg);
			  }
		 }
		 
		    @SuppressWarnings("deprecation")
		    public void stopThread(){
				mStopped = true;
				try {
//					this.stop();
					Thread.currentThread().interrupt();
				}
				catch(Exception e) {
					e.printStackTrace();
				}
	
			}
			
			public boolean isThreadActive(){
				return !mStopped;
			}
		    
		}

}
